﻿import java.util.ArrayList;
import java.util.Scanner;
import java.util.Stack;

//      String s = "(combine (combine (combine \"a()def\" \"cd\") \"cd\") (combine \"abcdef\" \"cd\"))";
//    	s = "(reverse \"a b c\")";
//	    s = "(search \"abcdef\" \"cd\")";
//    	s = "(search (combine \"123456\" \"abcdefgh\" \"123566\") (reverse \"dc\"))";
//		(quote (search (combine "1234567890" "abcdefgh" "1234567890") (reverse "dc")))

//		(quote "a bccd") "a bccd"
//		(reverse "d c") "c d"
//		(combine "cd" "fe" "ed") "cdfeed"
//		(search "abcde" "cd") "cde"

public class 华为2017第三题 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while (sc.hasNextLine()) {
			System.out.println(solve(sc.nextLine()));
		}
		sc.close();
	}

	static final String search = new String("(search ");
	static final String quote = new String("(quote ");
	static final String combine = new String("(combine ");
	static final String reverse = new String("(reverse ");

	static String solve(String s) {
		Stack<String> stk = new Stack<>();
		int len = s.length();
		int i = 0;
		ArrayList<String> al = new ArrayList<>();
		StringBuilder sb = new StringBuilder();

		while (i < len) {
			char c = s.charAt(i);

			// 分三种情况
			if (c == '(') {
				char c1 = s.charAt(i + 1);
				if (c1 == 's') {
					stk.push(search);
					i += search.length();
				} else if (c1 == 'q') {
					stk.push(quote);
					i += quote.length();
				} else if (c1 == 'c') {
					stk.push(combine);
					i += combine.length();
				} else if (c1 == 'r') {
					stk.push(reverse);
					i += reverse.length();
				} else {
					throw new RuntimeException();
				}
			} else if (c == '"') {
				int pi = i;
				i++;
				while (s.charAt(i) != '"')
					i++;
				stk.push(s.substring(pi + 1, i));
				i++;
			} else if (c == ')') {
				while (al.size() != 0) {
					al.remove(al.size() - 1);
				}
				while (true) {
					String oper = stk.pop();
					// 是运算符
					if (isOper(oper)) {
						if (oper == search) {
							if (al.size() != 2) {
								throw new RuntimeException();
							}
							int i0 = kmp(al.get(1), al.get(0));
							if (i0 == -1)
								stk.push("");
							else
								stk.push(al.get(1).substring(i0));
						} else if (oper == combine) {
							if (al.size() == 0) {
								throw new RuntimeException();
							}
							sb.delete(0, sb.length());
							for (int sbi = al.size() - 1; sbi > -1; sbi--) {
								sb.append(al.get(sbi));
							}
							stk.push(sb.toString());
						} else if (oper == quote) {
							if (al.size() != 1) {
								throw new RuntimeException();
							}
							stk.push(al.get(0));
						} else {
							if (al.size() != 1) {
								throw new RuntimeException();
							}
							sb.delete(0, sb.length());
							sb.append(al.get(0));
							stk.push(sb.reverse().toString());
						}
						break;
					} else {
						// 不是运算符
						al.add(oper);
					}
				}
				i++;
			} else if (c == ' ') {
				i++;
			} else {
				throw new RuntimeException();
			}
		}

		// 异常
		if (stk.size() != 1) {
			throw new RuntimeException();
		}

		// 输出
		return "\"" + stk.pop() + "\"";
	}

	// 判断字符串是不是定义的运算符
	static boolean isOper(String s) {
		if (s == search)
			return true;
		if (s == combine)
			return true;
		if (s == quote)
			return true;
		if (s == reverse)
			return true;
		return false;
	}

	// KMP算法
	private static int kmp(String s, String p) {
		int sn = s == null ? 0 : s.length();
		int pn = p == null ? 0 : p.length();
		if (pn == 0)
			return 0;
		if (sn == 0)
			return -1;
		int[] next = getNext(p, pn);
		int pi = 0, si = 0;
		while (si < sn)
			if (s.charAt(si) == p.charAt(pi)) {
				si++;
				pi++;
				if (pi == pn) {
					return si - pn;
				}
			} else if (next[pi] == -1) {
				si++;
			} else
				pi = next[pi];
		return -1;
	}

	private static int[] getNext(String p, int pn) {
		if (pn < 2)
			return new int[] { -1 };
		int[] next = new int[pn];
		next[0] = -1;
		next[1] = 0;
		int fi = 2, bi = 0;
		while (fi < pn) {
			if (p.charAt(fi - 1) == p.charAt(bi)) {
				next[fi++] = ++bi;
			} else if (bi <= 0) {
				next[fi++] = 0;
			} else
				bi = next[bi];
		}
		return next;
	}

}
